package com.uphone.magicleafteacher.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.blankj.utilcode.util.ScreenUtils;
import com.radish.baselibrary.dialog.OnDialogViewClickListener;
import com.radish.baselibrary.dialog.RadishDialog;
import com.radish.baselibrary.utils.ToastUtil;
import com.uphone.magicleafteacher.R;

public class DialogUtils {

    public interface Callback {
        void callback(RadishDialog dialog, View view, int type);
    }

    public static void saveAudioDialog(Context context, String audioUrl, Callback callback) {
        AudioPlayProgressUtil dialogAudioUtil = new AudioPlayProgressUtil();
        new RadishDialog.Builder(context)
                .setView(R.layout.dialog_sound_recording)
                .setWidth(ScreenUtils.getScreenWidth() * 5 / 6)
                .setCanceledOnTouchOutside(false)
                .setCanceledOnClickBack(true)
                .setClick(R.id.tv1, (dialog, view) -> {
                    if (dialogAudioUtil.getPlayView() == null)
                        dialogAudioUtil.setPlayView(view);
                    // 播放/暂停录音
                    if (!view.isSelected()) {
                        dialogAudioUtil.play(audioUrl);
                    } else {
                        dialogAudioUtil.stop();
                    }
                    if (callback != null)
                        callback.callback(dialog, view, 1);
                })
                .setOnDismissListener(dialog -> {
                    AudioPlayProgressUtil.stop();
                })
                .setClick(R.id.tv2, ((dialog, view) -> {
                    // 重新录制
                    dialog.dismiss();
                    if (callback != null)
                        callback.callback(dialog, view, 2);
                }))
                .setClick(R.id.tv3, ((dialog, view) -> {
                    // 提交音频
                    dialog.dismiss();
                    if (callback != null)
                        callback.callback(dialog, view, 3);
                }))
                .show();
    }

    public static void saveAudioDialogNoCommit(Context context, String audioUrl, Callback callback) {
        AudioPlayProgressUtil dialogAudioUtil = new AudioPlayProgressUtil();
        new RadishDialog.Builder(context)
                .setView(R.layout.dialog_sound_recording)
                .setWidth(ScreenUtils.getScreenWidth() * 5 / 6)
                .setVisible(R.id.tv3, View.GONE)
                .setVisible(R.id.dialog_cancel, View.VISIBLE)
                .setCanceledOnTouchOutside(false)
                .setCanceledOnClickBack(false)
                .setClick(R.id.tv1, (dialog, view) -> {
                    if (dialogAudioUtil.getPlayView() == null)
                        dialogAudioUtil.setPlayView(view);
                    // 播放/暂停录音
                    if (!view.isSelected()) {
                        dialogAudioUtil.play(audioUrl);
                    } else {
                        dialogAudioUtil.stop();
                    }
                    if (callback != null)
                        callback.callback(dialog, view, 1);
                })
                .setOnDismissListener(dialog -> {
                    AudioPlayProgressUtil.stop();
                })
                .setClick(R.id.tv2, ((dialog, view) -> {
                    // 重新录制
                    dialog.dismiss();
                    if (callback != null)
                        callback.callback(dialog, view, 2);
                }))
                .setClick(R.id.tv3, ((dialog, view) -> {
                    // 提交音频
                    dialog.dismiss();
                    if (callback != null)
                        callback.callback(dialog, view, 3);
                }))
                .setClick(R.id.dialog_cancel, ((dialog, view) -> {
                    dialog.dismiss();
                }))
                .show();
    }

    public static void alterDialog(Context context, String msg, Callback callback) {
        new RadishDialog.Builder(context).setView(com.radish.baselibrary.R.layout.dialog_alter)
                .setText(com.radish.baselibrary.R.id.dialog_message, CommonUtil.getStr(msg))
                .setWidth(ScreenUtils.getScreenWidth() * 4 / 5)
                .setClick(com.radish.baselibrary.R.id.dialog_submit, (dialog, view) -> {
                    dialog.dismiss();
                    if (callback != null)
                        callback.callback(dialog, view, 1);
                })
                .setClick(com.radish.baselibrary.R.id.dialog_cancel, (dialog, view) -> {
                    dialog.dismiss();
                }).show();
    }
}
